package org.huangrui.redis.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.huangrui.redis.entities.Customer;
import org.huangrui.redis.service.CustomerSerivce;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Random;

/**
 * @Author hr
 * @Create 2024-05-15 18:45
 */
@Slf4j
@RestController
@Tag(name = "客户Customer接口+布隆过滤器讲解")
public class CustomerController {
    @Resource
    private CustomerSerivce customerSerivce;

    @Operation(summary = "单个customer查询操作，按照customerid查询")
    @RequestMapping(value = "/customer/{id}", method = RequestMethod.GET)
    public Customer findCustomerById(@PathVariable Integer id) {
        return customerSerivce.findCustomerById(id);
    }

    @RequestMapping(value = "/customer/add", method = RequestMethod.POST)
    @Operation(summary = "数据库初始化2条Customer记录插入")
    public void addCustomer() {
        for (int i = 0; i < 2; i++) {
            Customer customer = new Customer();
            customer.setCname("customer" + i);
            customer.setAge(new Random().nextInt(30) + 1);
            customer.setPhone("1381111XXXX");
            customer.setSex((byte) new Random().nextInt(2));
            customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
            customerSerivce.addCustomer(customer);
        }
    }

    @Operation(summary = "BloomFilter，按照customerid查询")
    @RequestMapping(value = "/customerbloomfilter/{id}", method = RequestMethod.GET)
    public Customer findCustomerByIdWithBloomFilter(@PathVariable int id) {
        return customerSerivce.findCustomerByIdWithBloomFilter(id);
    }
}
